import { hiprint } from 'vue-plugin-hiprint'
import { JBoltApi } from '@/utils'

async function loadByUrl(sn: string, url: string) {
    //获取模板内容
    const { result: templateSn } = await JBoltApi.post(
        `/api/admin/hiprint/tpl/content?sn=${sn}`
    )
    const template = templateSn.data
    //获取模板Json
    const { result: json } = await JBoltApi.post(url)
    const templateData = json.data

    return {
        template,
        templateData
    }
}

async function loadByJson(sn: string, data: object) {
    //获取模板内容
    const { result: templateSn } = await JBoltApi.post(
        `/api/admin/hiprint/tpl/content?sn=${sn}`
    )
    const template = templateSn.data
    //获取模板Json
    const templateData = data
    return {
        template,
        templateData
    }
}

function printByUrl(sn: string, url: string, clientPrint = false) {
    loadByUrl(sn, url).then(({ template, templateData }) => {
        const hiprintTemplate = new hiprint.PrintTemplate({
            template: JSON.parse(template)
        })
        // 参数: 打印时设置 左偏移量，上偏移量
        const options = { leftOffset: -1, topOffset: -1 }
        // 扩展
        const ext = {
            callback: () => {
                console.log('浏览器打印窗口已打开')
            },
            styleHandler: () => {
                // 重写 文本 打印样式
            }
        }
        // 调用浏览器打印
        if (clientPrint) {
            if (hiprint.hiwebSocket.opened) {
                hiprintTemplate.print2(templateData, options, ext)
            } else {
                alert('请先连接客户端(刷新网页), 然后再点击「直接打印」')
            }
        } else {
            hiprintTemplate.print(templateData, options, ext)
        }
    })
}

function printByJson(sn: string, data: object, clientPrint = false) {
    loadByJson(sn, data).then(({ template, templateData }) => {
        const hiprintTemplate = new hiprint.PrintTemplate({
            template: JSON.parse(template)
        })
        // 参数: 打印时设置 左偏移量，上偏移量
        const options = { leftOffset: -1, topOffset: -1 }
        // 扩展
        const ext = {
            callback: () => {
                console.log('浏览器打印窗口已打开')
            },
            styleHandler: () => {
                // 重写 文本 打印样式
            }
        }
        // 调用浏览器打印
        if (clientPrint) {
            if (hiprint.hiwebSocket.opened) {
                hiprintTemplate.print2(templateData, options, ext)
            } else {
                alert('请先连接客户端(刷新网页), 然后再点击「直接打印」')
            }
        } else {
            hiprintTemplate.print(templateData, options, ext)
        }
    })
}

function toPdfByUrl(sn: string, url: string, name = '文件') {
    loadByUrl(sn, url).then(({ template, templateData }) => {
        const hiprintTemplate = new hiprint.PrintTemplate({
            template: JSON.parse(template)
        })
        hiprintTemplate.toPdf(templateData, name)
    })
}

function toPdfByJson(sn: string, data: object, name = '文件') {
    loadByJson(sn, data).then(({ template, templateData }) => {
        const hiprintTemplate = new hiprint.PrintTemplate({
            template: JSON.parse(template)
        })
        hiprintTemplate.toPdf(templateData, name)
    })
}

export const JBoltPrint = {
    printByUrl,
    printByJson,
    toPdfByUrl,
    toPdfByJson
}
